﻿using System;
using System.ComponentModel;
using System.Globalization;

namespace Gmantis.Controls
{
    internal class HierarchicalPresenterConverter : StringTypeConverter
    {
        internal HierarchicalPresenterConverter()
        {
        }

        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (!typeof(HierarchicalPresenter).IsAssignableFrom(sourceType))
            {
                return typeof(string).IsAssignableFrom(sourceType);
            }
            return true;
        }

        public override object ConvertFromString(ITypeDescriptorContext context, CultureInfo culture, string text)
        {
            return new HierarchicalPresenter { Header = text };
        }

        public override string ConvertToString(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            HierarchicalPresenter presenter = value as HierarchicalPresenter;
            if ((presenter != null) && (presenter.Header != null))
            {
                return presenter.Header.ToString();
            }
            return value.ToString();
        }
    }
}
